<?php
	class generalTransactionTypeController extends Controller{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Finance";
		public $subMenu = "";
		public $title = "";
		
		public function init() {
						
		}
		
		public function actionIndex(){
			$piutangParentId = AppSetting::getValue("pituang_acc_id");
			$piutangParent = MCoa::model()->findByPK($piutangParentId);
			$arrPiutangCoa =  MCoa::model()->findAll("acc_code like '". $piutangParent->acc_code .".%' and is_active = 1");			
			$data["arrPiutangCoa"] = $arrPiutangCoa; 
			$this->layout ="//layouts/ly_blank";
			$this->render('index',$data);
		}
		
		public function actionAllList(){
			
			
			$arrGeneralTransactionType =  MCustomerTransactionType::model()->findAll();
			
			$arrData = array();
			foreach($arrGeneralTransactionType as $customerTransactionType){
				$tmp = array();
				$tmp["id"]= $customerTransactionType->id;
				$tmp["name"]= $customerTransactionType->name;
				$tmp["piutang_acc_id"]= $customerTransactionType->piutang_acc_id;
				$tmp["piutang_acc_name"]= $customerTransactionType->piutangAcc->name;
				$tmp["is_active"]= $customerTransactionType->is_active;
				$tmp["sys_data"]= $customerTransactionType->sys_data;
				 
				$arrData[] = $tmp;
				//print_r($studentTransactionType);
			}
			$this->result["model"] = $arrData;

			$this->returnJSON($this->result);
			
			
			
		}		
		public function actionEdit(){
			$id = Yii::app()->request->getParam("id","");
			$name = Yii::app()->request->getParam("name","");
			$piutang_acc_id = Yii::app()->request->getParam("piutang_acc_id","");
			$is_active = Yii::app()->request->getParam("is_active","");
			$op = Yii::app()->request->getParam("op","");			
			if (strtolower($is_active) == ""){
				$is_active =0;
			}else{
				$is_active =1;
			}
			
			try{
				if ($id == ""){
					//new data
					$customerTransType = new MCustomerTransactionType();
					$customerTransType->name = $name;
					$customerTransType->piutang_acc_id = $piutang_acc_id;
					$customerTransType->is_active = $is_active;
					if(!$customerTransType->save()) {
						$this->result["actionMessage"] = "Gagal menyimpan data transaksi";
						throw new Exception(Yii::t("dialog",json_encode($customerTransType->getErrors())));
					}
				}else{
					//edit data
					if($op == "del"){
						$customerTransType = MCustomerTransactionType::model()->findByPK($id);
						if($customerTransType->sys_data){
							$this->result["actionMessage"] = "Data system ini tidak dapat di hapus";
							throw new Exception(Yii::t("dialog","data with id $id is locked.."));
						}else{
							if (!$customerTransType->delete()){
								$this->result["actionMessage"] = "Data ini tidak dapat di hapus";
								throw new Exception(Yii::t("dialog",json_encode($customerTransType->getErrors())));
							}	
						}
					}else{
						$customerTransType = MCustomerTransactionType::model()->findByPK($id);
						$customerTransType->name = $name;
						$customerTransType->piutang_acc_id = $piutang_acc_id;
						$customerTransType->is_active = $is_active;
						if(!$customerTransType->save()) {
							$this->result["actionMessage"] = "Gagal menyimpan data transaksi";
							throw new Exception(Yii::t("dialog",json_encode($customerTransType->getErrors())));
						}
					}
				}
				$this->result["actionStatus"] = 0;
			}catch (Exception $e){
				//array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());
				$this->result["actionStatus"] = 1;				 
				$this->result["actionError"] = $e->getMessage();
			}
			$this->returnJSON($this->result);
			
		}
		
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	}
?>